/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJInternalFrame.java
 *
 * Created on 05/12/2011, 06:11:02 PM
 */
package appbastanteo1.view;

import appbastanteo1.model.Representante;
import appbastanteo1.controller.AdmRepresentantes;
import appbastanteo1.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author JuanJose.Fuchs
 */
public class VistaRepresentantes extends javax.swing.JInternalFrame {

    private AdmRepresentantes admRepresentantes;
    private DefaultTableModel mdlRepresentante;

    /** Creates new form NewJInternalFrame */
    public VistaRepresentantes(AdmRepresentantes admRepresentantes) {
        initComponents();
        this.admRepresentantes = admRepresentantes;
        mdlRepresentante = (DefaultTableModel) tblRepresentante.getModel();
        mostrarRepresentante();
    }

    private void mostrarRepresentante() {
        mdlRepresentante.setRowCount(admRepresentantes.obtenerNumeroRepresentante());
        int fila = 0;
        for (Representante rpr : admRepresentantes.getRepresentante()) {
            mdlRepresentante.setValueAt(rpr.getTipoDocumento(), fila, 0);
            mdlRepresentante.setValueAt(rpr.getNroDocumento(), fila, 1);
            mdlRepresentante.setValueAt(rpr.getNombres(), fila, 2);
            mdlRepresentante.setValueAt(rpr.getApellidos(), fila, 3);
            mdlRepresentante.setValueAt(rpr.getCargo(), fila, 4);
            mdlRepresentante.setValueAt(rpr.getGrupoBastanteo(), fila, 5);
            fila++;
        }
    }

    private void limpiarFormulario() {
        cmbTipoDocumento.setSelectedIndex(0);
        txtNroDocumento.setText("");
        txtNombre.setText("");
        txtApellidos.setText("");
        cmbCargo.setSelectedIndex(0);
        cmbGrupoBastanteo.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnModificar = new javax.swing.JButton();
        txtApellidos = new javax.swing.JTextField();
        btnGrabar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtNroDocumento = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cmbCargo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cmbTipoDocumento = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblRepresentante = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        cmbGrupoBastanteo = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("Representantes");

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnGrabar.setText("Insertar");
        btnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGrabarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        jLabel3.setText("Nombre(s)");

        jLabel4.setText("Apellidos");

        jLabel5.setText("Cargo");

        cmbCargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Abogados", "Representante Legal" }));

        jLabel1.setText("Tipo de Documento");

        cmbTipoDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DNI", "Pasaporte", "RUC" }));

        jLabel2.setText("Nro de Documento");

        tblRepresentante.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Tipo de Documento", "Nro de Documento", "Nombre(s)", "Apellidos", "Cargo", "Grupo de Bastanteo"
            }
        ));
        jScrollPane1.setViewportView(tblRepresentante);

        jLabel6.setText("Grupo de Bastanteo");

        cmbGrupoBastanteo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Grupo A", "Grupo B", "Grupo C" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 769, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNroDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(8, 8, 8)
                                .addComponent(cmbTipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmbGrupoBastanteo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtApellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
                                    .addComponent(txtNombre))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGrabar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 749, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 301, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbTipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNroDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(cmbCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbGrupoBastanteo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(btnGrabar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar)
                    .addComponent(btnModificar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        int fila = tblRepresentante.getSelectedRow();
        if (fila != -1) {
            String nroDocumento = mdlRepresentante.getValueAt(fila, 1).toString();
            Representante rpr = admRepresentantes.obtenerRepresentante(nroDocumento);
            cmbTipoDocumento.setSelectedItem(rpr.getTipoDocumento());
            txtNroDocumento.setText(rpr.getNroDocumento());
            txtNombre.setText(rpr.getNombres());
            txtApellidos.setText(rpr.getApellidos());
            cmbCargo.setSelectedItem(rpr.getCargo());
            cmbGrupoBastanteo.setSelectedItem(rpr.getGrupoBastanteo());
            btnGrabar.setText("Guardar");
            cmbTipoDocumento.disable();
            txtNroDocumento.disable();
        }
}//GEN-LAST:event_btnModificarActionPerformed

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
        // TODO add your handling code here:
        String tipoDocumento = cmbTipoDocumento.getSelectedItem().toString();
        String nroDocumento = txtNroDocumento.getText();
        String nombre = txtNombre.getText();
        String apellidos = txtApellidos.getText();
        String cargo = cmbCargo.getSelectedItem().toString();
        String grupoBastanteo = cmbGrupoBastanteo.getSelectedItem().toString();
        String operacion = btnGrabar.getText();
        try {
            if (operacion.equals("Insertar")) {
                admRepresentantes.registrarRepresentante(tipoDocumento, nroDocumento, nombre, apellidos, cargo, grupoBastanteo);
                mostrarRepresentante();
                limpiarFormulario();
                JOptionPane.showMessageDialog(rootPane, "Representante agregado satisfactoriamente");
            } else {
                Representante rpr = admRepresentantes.obtenerRepresentante(nroDocumento);
                rpr.setNombres(nombre);
                rpr.setApellidos(apellidos);
                rpr.setCargo(cargo);
                rpr.setGrupoBastanteo(grupoBastanteo);
                mostrarRepresentante();
                limpiarFormulario();
                btnGrabar.setText("Insertar");
                txtNroDocumento.enable();
                cmbTipoDocumento.enable();
            }
        } catch (BusinessException ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
        }

}//GEN-LAST:event_btnGrabarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        int fila = tblRepresentante.getSelectedRow();
        if (fila != -1) {
            String nroDocumento = mdlRepresentante.getValueAt(fila, 1).toString();
            admRepresentantes.eliminarRespresentante(nroDocumento);
            mostrarRepresentante();
            JOptionPane.showMessageDialog(rootPane, "Representante eliminado");
        }
}//GEN-LAST:event_btnEliminarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGrabar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JComboBox cmbCargo;
    private javax.swing.JComboBox cmbGrupoBastanteo;
    private javax.swing.JComboBox cmbTipoDocumento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblRepresentante;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNroDocumento;
    // End of variables declaration//GEN-END:variables
}
